package com.atguigu01.collection;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CirclePointsDemo6 extends JPanel {
    private static final int RADIUS = 300; // 设置圆的半径
    private static final int DIVISIONS = 12; // 设置等分数量
    private static final int[] CIRCLE_RADII = {60, 120, 180, 240}; // 设置4个同心圆的半径
    private static final int POINTS_PER_DIVISION = 83; // 设置每个扇形中的点数
    private static final int POINT_RADIUS_DIFF = 10; // 设置同心圆之间的半径差值

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // 将Graphics对象转换为Graphics2D对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 开启抗锯齿

        int centerX = getWidth() / 2; // 获取面板中心横坐标
        int centerY = getHeight() / 2; // 获取面板中心纵坐标

        g2d.setColor(Color.RED); // 设置坐标颜色为红色

        // 绘制4个同心圆及其中的点
        for (int i = 0; i < CIRCLE_RADII.length; i++) {
            int circleRadius = CIRCLE_RADII[i]; // 获取当前同心圆的半径
            int pointRadiusDiff = POINT_RADIUS_DIFF * i; // 计算当前同心圆与上一个同心圆的半径差值
            int pointRadius = circleRadius - pointRadiusDiff; // 计算当前同心圆中点的半径
            int pointCount = POINTS_PER_DIVISION * (i + 1); // 计算当前同心圆中点的总数

            // 绘制同心圆
            g2d.setColor(Color.BLACK); // 设置同心圆的颜色为黑色
            g2d.drawOval(centerX - circleRadius, centerY - circleRadius, circleRadius * 2, circleRadius * 2);

            // 绘制同心圆中的点
            g2d.setColor(Color.RED); // 设置坐标颜色为红色
            for (int j = 0; j < pointCount; j++) {
                double angle = 2 * Math.PI * j / pointCount; // 计算当前点的角度
                int x = centerX + (int) (pointRadius * Math.cos(angle)); // 计算当前点的横坐标
                int y = centerY + (int) (pointRadius * Math.sin(angle)); // 计算当前点的纵坐标

                // 绘制当前点
                g2d.fillRect(x, y, 1, 1);
            }
        }

        // 绘制最外层的圆
        int diameter = 2 * RADIUS; // 计算圆的直径
        int x = centerX - RADIUS; // 计算圆的左上角横坐标
        int y = centerY - RADIUS; // 计算圆的左上角纵坐标
        g2d.setColor(Color.BLACK); // 设置圆的颜色为黑色
        g2d.drawOval(x, y, diameter, diameter); // 绘制圆

        // 绘制圆心到圆周上每个扇形的分割线
        for (int i = 0; i < DIVISIONS; i++) {
            double angle1 = 2 * Math.PI * i / DIVISIONS; // 计算扇形的起始角度
            double angle2 = 2 * Math.PI * (i + 1) / DIVISIONS; // 计算扇形的终止角度
            int x1 = centerX + (int) (RADIUS * Math.cos(angle1)); // 计算扇形起始点横坐标
            int y1 = centerY + (int) (RADIUS * Math.sin(angle1)); // 计算扇形起始点纵坐标
            int x2 = centerX + (int) (RADIUS * Math.cos(angle2)); // 计算扇形终止点横坐标
            int y2 = centerY + (int) (RADIUS * Math.sin(angle2)); // 计算扇形终止点纵坐标
            g2d.setColor(Color.BLACK); // 设置分割线颜色为黑色
            g2d.drawLine(centerX, centerY, x1, y1); // 绘制分割线
        }

        // 遍历圆心左边和1000个坐标
        for (int i = -500; i <= centerX; i++) {
            double yValue = Math.sqrt(RADIUS * RADIUS - (i - centerX) * (i - centerX)); // 根据圆的方程求解y值
            int y1 = centerY - (int) yValue; // 计算坐标1的纵坐标
            int y2 = centerY + (int) yValue; // 计算坐标2的纵坐标

            // 绘制坐标1
            g2d.setColor(Color.BLUE);
            g2d.fillRect(i, y1, 1, 1);

            // 绘制坐标2
            g2d.setColor(Color.GREEN);
            g2d.fillRect(i, y2, 1, 1);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Points Demo");
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CirclePointsDemo6 panel = new CirclePointsDemo6();
        frame.add(panel);
        frame.setVisible(true);
    }
}